package org.one.stone.soup.grfxML.plugin.function;


import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class StateWatcher extends SimplePlugin {


	public static final int ARG_WATCH = 0;

	public static final int ARG_SETTRUE = 1;

	public static final int ARG_SETFALSE = 2;



	org.one.stone.soup.grfxML.DataState watch = new org.one.stone.soup.grfxML.DataState();
	org.one.stone.soup.grfxML.DataState setTrue = new org.one.stone.soup.grfxML.DataState();
	org.one.stone.soup.grfxML.DataState setFalse = new org.one.stone.soup.grfxML.DataState();
	boolean currentState;
	boolean waitingReset = true;

/**
 * StateWatcher constructor comment.
 */
public StateWatcher(GrfxMLEngine engine){
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
	currentState = watch.getValue();
}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(watch.getValue()!=currentState)
	{
		currentState = watch.getValue();
		waitingReset = true;
		if(currentState==true)
			setTrue.setValue( true,this );
		else
			setFalse.setValue( true,this );
	}
	else if(waitingReset==true)
	{
		waitingReset = false;
		setTrue.setValue( false,this );
		setFalse.setValue( false,this );
	}
 }catch(LockException le){}
}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	watch = grfxMLCaster.cast(watch,getArg(watch,ARG_WATCH,store));

	setTrue = grfxMLCaster.cast(setTrue,getArg(setTrue,ARG_SETTRUE,store));

	setFalse = grfxMLCaster.cast(setFalse,getArg(setFalse,ARG_SETFALSE,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	watch = grfxMLCaster.replace(watch,oldObj,newObj);

	setTrue = grfxMLCaster.replace(setTrue,oldObj,newObj);

	setFalse = grfxMLCaster.replace(setFalse,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
